package com.emar.spring.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emar.spring.common.Constant;
import com.emar.spring.common.util.StringUtil;
import com.emar.spring.dto.ChartDTO;
import com.emar.spring.dto.ParamsDTO;
import com.emar.spring.dto.SingleDataDTO;
import com.emar.spring.service.RecodeService;
import com.emar.spring.service.SystemService;

/**
 * 系统操作类 
 */
@Controller
@RequestMapping(value="system")
public class SystemController {
	@Autowired
	private SystemService systemService;
	@Autowired
	private RecodeService recodeService;
	
	@RequestMapping(value="/getsystemlist",method=RequestMethod.POST)
	@ResponseBody
	public String getProjectList(
			@ModelAttribute ParamsDTO pe,
			@RequestParam(value="page",defaultValue="0") int page,
			@RequestParam(value="rows",defaultValue="10") int rows,
			Model model,ServletRequest request){
//		pe.setSecurityLevelCode(securityLevelCode);
		
		int offset = (page-1)*rows;
		int total = systemService.getSystemCount(pe, pe.getGroupBy(), offset, rows);
		List<ParamsDTO> proList = systemService.getParamsDTOPageList(pe, pe.getGroupBy(), offset, rows);
		
		JSONArray arr = new JSONArray(proList);
		JSONObject data = new JSONObject();
		data.put( "total", total);
		data.put( "rows", arr);
		
		return data.toString();
	}
	
	/**
	 * 获取数据，封装为json格式
	 * @param groupBy
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/systempie",method=RequestMethod.POST)
	public String ownerGetByDayPie(
			@ModelAttribute ParamsDTO dto,
			Model model,ServletRequest request){
		ChartDTO chart = new ChartDTO( "系统符合率","", "",  " (s)",  "", "0");
		JSONObject json = new JSONObject();
		
		dto.setIsValid( "是");				//适用
		
		List<ParamsDTO> list = recodeService.getProjectDTOCountList(dto,"RecordJudge");	//把“该系统下所有对象的所有测评指标的总和-不适用的问题总数”作为问题总量,即适用的问题量；
		if(list.size()>0 && list !=null){
			ParamsDTO d  = list.get(0);
			chart.setSubcaption(d.getCommissionCorpName()+"-"+d.getSystemName());
		}
		
		List<SingleDataDTO> singleList = new ArrayList<SingleDataDTO>();
		for(ParamsDTO r :list){
			SingleDataDTO s = new SingleDataDTO();
			s.setLabel( r.getCommissionCorpName()+"-"+r.getSystemName()+"-"+r.getRecordJudge());
			s.setValue( String.valueOf(r.getCount()));
			singleList.add(s);
		}
		JSONObject chart1 = new JSONObject(chart);
		json.put( "chart",  chart1);
		
		json.put( "data", singleList);
		model.addAttribute( "json", json);
		model.addAttribute( "url",  "systempie");
		return "chartdemo/pie";
	}
	
	/**
	 * 获取单个系统的风险比率，封装为json格式
	 * @param groupBy
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/singlesystempie",method=RequestMethod.POST)
	public String getSingleSystemSecurityPie(
			@ModelAttribute ParamsDTO dto,
			Model model,ServletRequest request){
		ChartDTO chart = new ChartDTO( "单系统风险率","", "",  " (s)",  "", "0");
		JSONObject json = new JSONObject();
		
		dto.setIsValid( "是");				//适用
		dto.setGroupBy( "t.`SecurityLevelCode`");
		List<ParamsDTO> list = systemService.getSingleSystemSecuList(dto, 0, 0);	//把“该系统下所有对象的所有测评指标的总和-不适用的问题总数”作为问题总量,即适用的问题量；
		if(list.size()>0 && list !=null){
			ParamsDTO d  = list.get(0);
			chart.setSubcaption(d.getCommissionCorpName()+"-"+d.getSystemName());
		}
		
		List<SingleDataDTO> singleList = new ArrayList<SingleDataDTO>();
		String level = "";
		for(ParamsDTO r :list){
			SingleDataDTO s = new SingleDataDTO();
			s.setLabel( r.getCommissionCorpName()+"-"+r.getSystemName()+"-"+this.getLevel(r.gettSecurityLevelCode()));
			s.setValue( String.valueOf(r.getCount()));
			singleList.add(s);
		}
		JSONObject chart1 = new JSONObject(chart);
		json.put( "chart",  chart1);
		
		json.put( "data", singleList);
		model.addAttribute( "json", json);
		model.addAttribute( "url",  "singlesystempie");
		return "chartdemo/pie";
	}
	
	private String getLevel(int i){
		switch(i){
			case 1:return Constant.LEVEL_1;
			case 2:return Constant.LEVEL_2;
			case 3:return Constant.LEVEL_3;
			case 4:return Constant.LEVEL_4;
			case 5:return Constant.LEVEL_5;
			default :return Constant.LEVEL_Un;
		}
	}
	/**
	 * 获取全部系统的风险比率，封装为json格式
	 * @param groupBy
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/allsystempie",method=RequestMethod.POST)
	public String getAllSystemSecurityPie(
			@ModelAttribute ParamsDTO dto,
			Model model,ServletRequest request){
		ChartDTO chart = new ChartDTO( "全系统风险率","", "",  " (s)",  "", "0");
		JSONObject json = new JSONObject();
		
		dto.setGroupBy( " s.`SecurityLevelCode` ");
		List<ParamsDTO> list = systemService.getAllSystemSecuList(dto, "s.`SecurityLevelCode`", 0,0);//把“该系统下所有对象的所有测评指标的总和-不适用的问题总数”作为问题总量,即适用的问题量；
		
		List<SingleDataDTO> singleList = new ArrayList<SingleDataDTO>();
		for(ParamsDTO r :list){
			SingleDataDTO s = new SingleDataDTO();
			s.setLabel(this.getLevel(r.getSecurityLevelCode()));
			s.setValue( String.valueOf(r.getCount()));
			singleList.add(s);
		}
		JSONObject chart1 = new JSONObject(chart);
		json.put( "chart",  chart1);
		
		json.put( "data", singleList);
		model.addAttribute( "json", json);
		model.addAttribute( "url",  "allsystempie");
		return "chartdemo/pie";
	}
	
}
